/**
 * Created by yangfan on 2016/11/23.
 */
define(['config/appregister',
		"i18n",
		"jquery",
		"business/myResume/services/httpServices",
		"business/services/utils",	
		],
function(app,i18n,$,httpServices,utils,constant){
    app.directive("educationExpression",function(){
        return {
            templateUrl:"app/business/myResume/templates/educationExpression.html",
            scope:{
                conf:"=confData"
            },
            link:function(scope,element,attrs){
             scope.i18n = i18n;   
             scope.curI18n = i18n.resume;
             scope.isShow  = {
             	workStart:false,
             	workEnd:false,
             	
             };
              var selectStyleSmall = {"background-color":"#fff", "width":"90px"},
        		  selectStyleBig = {"background-color":"#fff", "width":"200px"};
        	
        function getValidateArr(form,topKey){
            if(!angular.isObject(form)){
                return ;
            };
            var itemArr = [];
            for(var key in form ){
                if(key.indexOf("$")<0){
                    itemArr.push(key);
                }
            }
           return  getValidateItem(topKey,itemArr);
        }
        function getValidateItem(topKey,itemArr){
            if( toString.call(itemArr) != '[object Array]'){
                return
            }
            var flag =false;
            itemArr.forEach(function(item ){
                if(scope[topKey][item].$invalid == true){
                    scope[topKey][item].$pristine = false;
                    flag = true;
                }
            });
            return flag ;
        }
        	 
        	 scope.showComponent = function(key){
           		  scope[key].isShow = !scope[key].isShow ;
        		};
        	
        	 scope.changeShow = function(key){
            	if(scope.isShow.hasOwnProperty(key)){
                scope.isShow[key] = !scope.isShow[key];
           	 }
             };
                scope.curentCalendarLeave = function(){
                    scope.isShow.workStart = false ;
                    scope.isShow.workEnd = false ;
                }
                //工作经验
         
        scope.school = "";
        scope.overseas = "";
        scope.majorDes = "";
       		  //工作时间
      	scope.educationExpression = {
            startValue:"",
            endValue:"",
            isShow:false
        	};    
        	//专业
        scope.majorConf = {
            top:"",
            second:"",
            detail:"",
            isShow:false,
        	};
            	       	
        scope.educationSelect = {
           	//学历
        	education:{
        		model:"",
                value:scope.curI18n.placeholderSelect,
                key:"dictName",
                style:selectStyleBig
        	},
        
        	errMessage:{
        		education:false,
        		school:false,
        	}       	       	      	       	       	
        };
        	
        	//获取最低学历       
        	function getJobEducation(){
            		var req = {
            			type:"GET",
            			uri:"getJobEducation"
            		};
            		httpServices.jobServices(req).success(function(response){ 
            			scope.educationSelect.education.model = response.data;
            		}).error(function(){})           		
            	}
        	function init(){
        	   		getJobEducation();
        	}

        	scope.educationExpressionSubmit = function(){
        		if(scope.educationExpressionForm.$invalid){
                var topKey = "educationExpressionForm";
                var flag = getValidateArr(scope.educationExpressionForm,topKey);
                //学历 学校校验
                if(scope.educationSelect.education.value== scope.curI18n.placeholderSelect){	
                    scope.educationSelect.errMessage.education = true;
                };
                if(scope.school ==""){
                	scope.educationSelect.errMessage.school = true;
                }
                

            }else{
            	var guildObj = {
            		dictId:[]
            	};
            	var fcObj = {
            		dictId:[]
            	};

            	var req = {
            		type:"POST",
            		uri:"saveEduExp",
            		data:{
                        startTime:utils.YMD2YMD(scope.educationExpression.startValue),
                        endTime:utils.YMD2YMD(scope.educationExpression.endValue),
                        //学历学位
                        degreeId:scope.educationSelect.education.value.dictId,
                        eduName:scope.school,
                        //fulltimeStatus:"",
                        //专业
                        professionalId:scope.majorConf.detail.pdcId,
                        overseas:scope.overseas,
                        introduction:scope.majorDes
            		}
            	};
                    if(scope.isUpdate){
                        req.uri = "updateEduExp";
                        req.data.eeId = scope.conf.eeId;
                        req.data.cvdId = scope.conf.cvdId;
                    }
            	 httpServices.getCvdatabases(req).success(function(response){
                        if(response.msg == "success"){
                            Back(0);
                        }

                }).error(function(response){
                     Back(0);
                })
            	
            }
        	};

                scope.cancelEduExpressionSubmit = function(){
                    Back(1);
                };
                function Back(type){
                    if(scope.isUpdate == true){
                        var obj = {
                            key1:"showEducationExpression",
                            key2:"showForm",
                            type:type
                        }
                    }else{
                        var obj = {
                            key1:"isInitAddNew",
                            key2:"educationExpression",
                            type:type
                        }
                    }
                    scope.$emit("reGetResumeInfo",obj);
                }

                // 表示是否修改
                scope.isUpdate = false ;
                if(typeof scope.conf!="undefined"){
                    fillData()
                }else{
                    init();
                }
                function fillData(){

                    scope.educationExpression.startValue = utils.getYMD(scope.conf.startValue);
                    scope.educationExpression.endValue = utils.getYMD(scope.conf.endValue);

                    scope.majorConf.detail = scope.conf.majorConf;
                    scope.school = scope.conf.school;
                    scope.majorDes = scope.conf.introduction;

                    //学历学位
                    scope.educationSelect.education.value = getEduConf(scope.conf.degreeId);
                    scope.overseas = scope.conf.overseas;
                    scope.isUpdate = true ;
                    init();

                }

                function getEduConf (degreeId){
                    if(degreeId == 4){
                        return {
                            dictId:degreeId,
                            dictName:"本科"
                        }
                    }
                    if(degreeId == 3){
                        return {
                            dictId:degreeId,
                            dictName:"专科"
                        }
                    }
                }
                
             
            }
        }
    })
});